<template>
  <div class="login-container">
    <h2>登录</h2>
    <form @submit.prevent="handleLogin">
      <div class="form-group">
        <label>用户名</label>
        <input v-model="form.username" type="text" placeholder="请输入学号" />
      </div>
      <div class="form-group">
        <label>密码</label>
        <input v-model="form.password" type="password" placeholder="请输入密码" />
      </div>
      <button type="submit">登录</button>
    </form>
  </div>
</template>

<script>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { useAuthStore } from '@/stores/auth';

export default {
  setup() {
    const router = useRouter();
    const authStore = useAuthStore();

    const form = ref({
      username: '23003012', // 默认学号
      password: '123456'    // 默认密码
    });
const handleLogin = async () => {
  try {
    const username = form.value.username.trim();
    const password = form.value.password.trim();
    
    if (!username || !password) {
      alert('学号和密码不能为空');
      return;
    }

    await authStore.loginUser({ 
      username, 
      password 
    });
    router.push('/articles');
  } catch (error) {
    console.error('登录错误详情:', error);
    alert(`登录失败: ${error.message || '学号或密码错误'}`);
  }
};

    return { form, handleLogin };
  }
};
</script>

<style scoped>
.login-container {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
}
.form-group {
  margin-bottom: 15px;
}
input {
  width: 100%;
  padding: 8px;
  margin-top: 5px;
}
button {
  width: 100%;
  padding: 10px;
  background-color: #42b983;
  color: white;
  border: none;
  cursor: pointer;
}
</style>